package com.able.concurrency.t5;

import java.util.concurrent.*;

/**
 * @author abel.he
 * @data 2018/10/29 13:10
 * @describe
 *  多线程返回值
 */
public class Test3_Future {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*
        // 获取线程返回的数据
        FutureTask<String> futureTask = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Thread result";
            }
        });

        new Thread(futureTask).start();
        System.out.println(futureTask.get());
        */

        ExecutorService service = Executors.newFixedThreadPool(1);
        Future<String> future = service.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread execute");
                return "Thread result";
            }
        });

        System.out.println(future);
        // 查看线程是否结束，任务是否完成，call方法是否执行结束
        System.out.println(future.isDone());

        // 获取线程返回值
        System.out.println(future.get());
        System.out.println(future.isDone());

    }

}
